/**
 * Copyright 2014 the original author or authors. All rights reserved.
 */
package com.visionet.security.realm;

import com.visionet.security.base.constants.Constants;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authc.AuthenticationToken;

import com.visionet.security.utils.CipherUtils;

/**
 * 无状态登录信息
 *
 * @author suxiaojing
 * @since 0.0.1
 */
public class StatelessToken implements AuthenticationToken {
    /**
     *
     */
    private static final long serialVersionUID = 6852572649382715613L;

    private String token;

    private String key;

    private String userId;

    private String userName;

    private String loginName;

    private Long expireDate;

    public StatelessToken(String token, String key) {
        this.token = token;
        this.key = key;
        String original = CipherUtils.blowfishDecryptHex(token, key);
        if (StringUtils.isNotEmpty(original)) {
            String[] userInfo = StringUtils.split(original, Constants.STATELESS_SEPARATOR);
            this.userId = userInfo[0];
            this.userName = userInfo[1];
            this.loginName = userInfo[2];
            this.expireDate = Long.valueOf(userInfo[3]);
        }
    }

    /**
     * @return the token
     */
    public String getToken() {
        return token;
    }

    /**
     * @param token the token to set
     */
    public void setToken(String token) {
        this.token = token;

    }

    /**
     * @return the key
     */
    public String getKey() {
        return key;
    }

    /**
     * @param key the key to set
     */
    public void setKey(String key) {
        this.key = key;
    }

    /*
     * (non-Javadoc)
     *
     * @see org.apache.shiro.authc.AuthenticationToken#getPrincipal()
     */
    @Override
    public Object getPrincipal() {
        return getToken();
    }

    /*
     * (non-Javadoc)
     *
     * @see org.apache.shiro.authc.AuthenticationToken#getCredentials()
     */
    @Override
    public Object getCredentials() {
        return getKey();
    }

    /**
     * @return the userId
     */
    public String getUserId() {
        return userId;
    }

    /**
     * @param userId the userId to set
     */
    public void setUserId(String userId) {
        this.userId = userId;
    }

    /**
     * @return the userName
     */
    public String getUserName() {
        return userName;
    }

    /**
     * @param userName the userName to set
     */
    public void setUserName(String userName) {
        this.userName = userName;
    }

    /**
     * @return the loginName
     */
    public String getLoginName() {
        return loginName;
    }

    /**
     * @param loginName the loginName to set
     */
    public void setLoginName(String loginName) {
        this.loginName = loginName;
    }

    /**
     * @return the expireDate
     */
    public Long getExpireDate() {
        return expireDate;
    }

    /**
     * @param expireDate the expireDate to set
     */
    public void setExpireDate(Long expireDate) {
        this.expireDate = expireDate;
    }

}
